package com.yzxb.messystem.config;

import com.yzxb.messystem.entity.Product;
import com.yzxb.messystem.entity.ProductionRecord;
import com.yzxb.messystem.entity.WorkOrder;
import com.yzxb.messystem.mapper.ProductMapper;
import com.yzxb.messystem.mapper.ProductionRecordMapper;
import com.yzxb.messystem.mapper.WorkOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;

@Component
public class DataInitializer implements CommandLineRunner {
    
    @Autowired
    private ProductMapper productMapper;
    
    @Autowired
    private WorkOrderMapper workOrderMapper;
    
    @Autowired
    private ProductionRecordMapper productionRecordMapper;
    
    @Override
    public void run(String... args) throws Exception {
        // 检查是否已存在数据，如果不存在则初始化
        if (productMapper.selectCount() == 0) {
            // 初始化产品数据
            initializeProductData();
            
            // 初始化工单数据
            initializeWorkOrderData();
            
            // 初始化生产记录数据
            initializeProductionRecordData();
        }
    }
    
    private void initializeProductData() {
        Product product1 = new Product();
        product1.setProductCode("P001");
        product1.setProductName("智能手机A");
        product1.setDescription("高性能智能手机，6.7英寸屏幕，128GB存储");
        product1.setCategory("电子产品");
        product1.setPrice(3999.00);
        product1.setStockQuantity(100);
        
        Product product2 = new Product();
        product2.setProductCode("P002");
        product2.setProductName("笔记本电脑B");
        product2.setDescription("轻薄笔记本，16GB内存，512GB固态硬盘");
        product2.setCategory("电子产品");
        product2.setPrice(5999.00);
        product2.setStockQuantity(50);
        
        Product product3 = new Product();
        product3.setProductCode("P003");
        product3.setProductName("机械键盘C");
        product3.setDescription("RGB背光机械键盘，青轴");
        product3.setCategory("电脑配件");
        product3.setPrice(499.00);
        product3.setStockQuantity(200);
        
        // 设置创建时间
        product1.setCreateTimeDefault();
        product2.setCreateTimeDefault();
        product3.setCreateTimeDefault();
        
        // 保存示例产品数据
        productMapper.insert(product1);
        productMapper.insert(product2);
        productMapper.insert(product3);
        
        System.out.println("已初始化示例产品数据");
    }
    
    private void initializeWorkOrderData() {
        // 获取产品
        Product product1 = productMapper.findByProductCode("P001");
        Product product2 = productMapper.findByProductCode("P002");
        Product product3 = productMapper.findByProductCode("P003");
        
        if (product1 != null && product2 != null && product3 != null) {
            // 创建工单1
            WorkOrder order1 = new WorkOrder();
            order1.setOrderCode("WO001");
            order1.setProductId(product1.getId());
            order1.setPlannedQuantity(50);
            order1.setCompletedQuantity(25);
            order1.setRejectedQuantity(2);
            order1.setStatus(WorkOrder.OrderStatus.IN_PROGRESS);
            order1.setPlannedStartDate(LocalDateTime.now().minusDays(2));
            order1.setPlannedEndDate(LocalDateTime.now().plusDays(3));
            order1.setActualStartDate(LocalDateTime.now().minusDays(2));
            order1.setProductionLine("生产线A");
            order1.setCreateTimeDefault();
            
            // 创建工单2
            WorkOrder order2 = new WorkOrder();
            order2.setOrderCode("WO002");
            order2.setProductId(product2.getId());
            order2.setPlannedQuantity(20);
            order2.setCompletedQuantity(20);
            order2.setRejectedQuantity(0);
            order2.setStatus(WorkOrder.OrderStatus.COMPLETED);
            order2.setPlannedStartDate(LocalDateTime.now().minusDays(5));
            order2.setPlannedEndDate(LocalDateTime.now().minusDays(1));
            order2.setActualStartDate(LocalDateTime.now().minusDays(5));
            order2.setActualEndDate(LocalDateTime.now().minusDays(2));
            order2.setProductionLine("生产线B");
            order2.setCreateTimeDefault();
            
            // 创建工单3
            WorkOrder order3 = new WorkOrder();
            order3.setOrderCode("WO003");
            order3.setProductId(product3.getId());
            order3.setPlannedQuantity(100);
            order3.setCompletedQuantity(0);
            order3.setRejectedQuantity(0);
            order3.setStatus(WorkOrder.OrderStatus.PENDING);
            order3.setPlannedStartDate(LocalDateTime.now().plusDays(1));
            order3.setPlannedEndDate(LocalDateTime.now().plusDays(7));
            order3.setProductionLine("生产线C");
            order3.setCreateTimeDefault();
            
            // 保存示例工单数据
            workOrderMapper.insert(order1);
            workOrderMapper.insert(order2);
            workOrderMapper.insert(order3);
            
            System.out.println("已初始化示例工单数据");
        }
    }
    
    private void initializeProductionRecordData() {
        // 获取工单
        WorkOrder order1 = workOrderMapper.findByOrderCode("WO001");
        WorkOrder order2 = workOrderMapper.findByOrderCode("WO002");
        
        if (order1 != null && order2 != null) {
            // 为工单1创建生产记录
            ProductionRecord record1 = new ProductionRecord();
            record1.setWorkOrderId(order1.getId());
            record1.setQuantity(15);
            record1.setRecordType(ProductionRecord.RecordType.PRODUCTION);
            record1.setWorkstation("工作站A1");
            record1.setOperator("操作员001");
            record1.setShift("白班");
            record1.setQualityStatus("合格");
            record1.setCreateTime(LocalDateTime.now().minusDays(2));
            
            ProductionRecord record2 = new ProductionRecord();
            record2.setWorkOrderId(order1.getId());
            record2.setQuantity(10);
            record2.setRecordType(ProductionRecord.RecordType.PRODUCTION);
            record2.setWorkstation("工作站A1");
            record2.setOperator("操作员002");
            record2.setShift("中班");
            record2.setQualityStatus("合格");
            record2.setCreateTime(LocalDateTime.now().minusDays(1));
            
            ProductionRecord record3 = new ProductionRecord();
            record3.setWorkOrderId(order1.getId());
            record3.setQuantity(2);
            record3.setRecordType(ProductionRecord.RecordType.REJECTION);
            record3.setWorkstation("工作站A1");
            record3.setOperator("操作员001");
            record3.setShift("白班");
            record3.setQualityStatus("外观缺陷");
            record3.setRemarks("屏幕划痕");
            record3.setCreateTime(LocalDateTime.now().minusDays(2));
            
            // 为工单2创建生产记录
            ProductionRecord record4 = new ProductionRecord();
            record4.setWorkOrderId(order2.getId());
            record4.setQuantity(20);
            record4.setRecordType(ProductionRecord.RecordType.PRODUCTION);
            record4.setWorkstation("工作站B1");
            record4.setOperator("操作员003");
            record4.setShift("白班");
            record4.setQualityStatus("合格");
            record4.setCreateTime(LocalDateTime.now().minusDays(4));
            
            // 保存示例生产记录数据
            productionRecordMapper.insert(record1);
            productionRecordMapper.insert(record2);
            productionRecordMapper.insert(record3);
            productionRecordMapper.insert(record4);
            
            System.out.println("已初始化示例生产记录数据");
        }
    }
}